// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import DTTS from "../../dddd/DTTS";
import LRcontr from "../../登录/LRcontr";
import GMessage from "./GMessage";
import MessageCenter from "./MessageCenter";
import { SHMessage } from "./SHMessage";

const {ccclass, property} = cc._decorator;

@ccclass
export default class KHD extends cc.Component {
/*static ln:number=0;
static dn:number=0;
static Client:any=null;
static Gammer:any=null;
static JMessage= []; 
static PT:number[]=[0];//玩家序号，0为观战席
static xz:number=0
static FZ:boolean=false;//是否为房主
static WZ:cc.Node=null;
static FJN:boolean=true;
static id:number//唯一id
static juese:number[]=[];
static HB:number[]=[0,0,0];
static Cname:string="213";//用户名
//118.25.16.45:3001/
static ingame:number=0;
  static dat: any;
/////////////////打开游戏
static indl(){


 
    KHD.Client=io.connect("http://118.25.16.45:3001/",{
        reconnection: true,   // 启用自动重连
        reconnectionAttempts: 5,  // 最多尝试重连 5 次
        reconnectionDelay: 1000,  // 每次重连的时间间隔为 1 秒
        reconnectionDelayMax: 5000, // 每次重连的最大时间间隔为 5 秒
      });


      
      KHD.Client.on('connect',()=>{KHD.Client.emit('socketname',KHD.Cname);})


   
  
     cc.find("Canvas/New Label").active=true
  cc.find("Canvas/New Label").getComponent(cc.Label).string="没发或者发了运行不了"
      KHD.Client.on('connect_error', (err) => {
        console.log("true")
     cc.find("Canvas/New Label").active=true
       cc.find("Canvas/New Label").getComponent(cc.Label).string="没连上"
      });

}
//重连
static reconnect()
{
    KHD.Client.io.on('reconnect_attempt', (attemptNumber:any) => {
        console.log(`正在尝试第${attemptNumber}次重连...`);
    });

    KHD.Client.io.on('reconnect', (attemptNumber:any) => {
        console.log(`已重连至服务器，一共尝试了${attemptNumber}次`);
        // 执行自定义操作
    });
}

///////////////注册
static registered(account:string,password:string,email:string){
    let Data={playername:account,account:account,password:password,email:email}
KHD.Client.emit("registered_player",Data)//发送注册给服务器
//接收是否成功,成功会返回'registered_player_success'这个字符串
//否则就是'registered_player_fail'
KHD.Client.on('registered_player',(data:any)=>{
  console.log(data)
  if(data='registered_player_success'){
    //跳转回登录
    this.Rsback();
  }
})}



//////////////登录
static login(account:string,password:string){
  KHD.Cname=account
  console.log(KHD.Cname)
 
    let Data={account:account,password:password}
 KHD.Client.emit('login_player',Data)
 //登录其实就是获取,对应账号和密码的玩家的数据
 //找不到就是[],找得到就是对应的玩家数据[id,playername, account, password, email, coin, diamond, pigcoin, rankpoint, experiencepoint]
 //这个id很重要,每个玩家都只有唯一一个,也是后面用来查找的关键数据,登录后一定要记录下来
 KHD.Client.on('login_player',(data:any)=>{
  console.log(data);

  //外加的
  /*if(data==null){
    return false;
  }
 */
 /* console.log(data[0].coin)
  console.log(data[0].playername)
 KHD.Cname=data[0].playername
 KHD.id=data[0].id
 KHD.HB[0]=data[0].coin
 KHD.HB[1]=data[0].diamond
 KHD.HB[2]=data[0].pigcoin
 KHD.juese=[];
 console.log(KHD.HB)
 const propKeys = Object.keys(data); // 获取属性键数组
    const propValues = [];
    for (let i = 1; i < propKeys.length; i++) { // 从第二个属性开始遍历
        const propName = propKeys[i];
       KHD.juese.push(Data[propName]);
    }

    cc.find("Canvas/登录/load").getComponent(LRcontr).HBFZ();
 })
//外加*/
/*
}



static inDT(name:string){
    KHD.Client=io.connect("http://118.25.16.45:3001/",{
        reconnection: true,   // 启用自动重连
        reconnectionAttempts: 5,  // 最多尝试重连 5 次
        reconnectionDelay: 1000,  // 每次重连的时间间隔为 1 秒
        reconnectionDelayMax: 5000, // 每次重连的最大时间间隔为 5 秒
      });
    KHD.Cname=name;
    KHD.Client.emit('socketname',KHD.Cname);

}










//////////////////////////////////////////////////////////////////////!!!!!!!!!!!!!!!!!!!!!!!!!!11





static updateHB(){
    
    KHD.Client.emit('update_money',{coin:KHD.HB[0],diamond:KHD.HB[1],pigcoin:KHD.HB[2],player_id:KHD.id})
    KHD.Client.on('update_money',(data:any)=>{
        console.log(data)
    })
}
static updateJuese(c:number){
    let a='c'+c
    console.log(a)
    let Data={which_character_update:a,c_value:1,player_id:KHD.id}
    KHD.Client.emit('update_character_relevance',Data)
    KHD.Client.on('update_character_ relevance',(data:any)=>{
        console.log(data)
    })

    //更新一下Juese
}




static ST(){
    KHD.Client.emit('ST',KHD.id)
    KHD.Client.on('ST',(data:any)=>{

////与登录相同的刷新
KHD.Cname=data[0].playername
 KHD.id=data[0].id
 KHD.HB[0]=data[0].coin
 KHD.HB[1]=data[0].diamond
 KHD.HB[2]=data[0].pigcoin

 console.log(KHD.HB)




    })

}

///////////////////////////////////////////!!!!!!!


static creatRoom(FJname:string,PassW:string){
    KHD.Client.emit('createroom',[FJname,PassW,KHD.Cname,1]);
   KHD.Client.on('createroomTF',(data:any)=>{
switch (data) {
    case 'T':KHD.FZ=true;KHD.inRoom(FJname,PassW);  
        
        break;
        case 'F':cc.find("Canvas/CHU").children[2].getComponent(DTTS).WZTS("创建房间失败");
        
        break;

    default:
        break;
}

   })
   
  }

static inRoom(FJname:string,PassW:string){
    

     KHD.Client.on('connectroom',(data:any)=>{
        console.log(data)
        cc.find("Canvas/New Node/New Label").getComponent(cc.Label).string=FJname
switch (data[1]) {
    case 'F': cc.find("Canvas/CHU").children[2].getComponent(DTTS).WZTS("房间号或密码错误");
        
        break;
       
      

    default:
        KHD.Gammer=io.connect('http://118.25.16.45:3001/'+data[1],{
            reconnection: true,   // 启用自动重连
            reconnectionAttempts: 5,  // 最多尝试重连 5 次
            reconnectionDelay: 1000,  // 每次重连的时间间隔为 1 秒
            reconnectionDelayMax: 5000, // 每次重连的最大时间间隔为 5 秒
          });

    cc.find("Canvas/CHU").active=false;
    //房间重连
    KHD.Gammer.io.on('reconnect_attempt', (attemptNumber:any) => {
        console.log(`正在尝试第${attemptNumber}次重连...`);
    });

    KHD.Gammer.io.on('reconnect', (attemptNumber:any) => {
        console.log(`已重连至服务器，一共尝试了${attemptNumber}次`);
        // 执行自定义操作
    });
   KHD.Gammer.emit('gammername',[KHD.Cname,0,0,0]);
   KHD.Gammer.on('startBP',()=>{KHD.BPstart()})
   KHD.Gammer.on('all_people',(data:any)=>{
let c=0
    for(let a of data){
        if (a[2]!=0) {
            cc.find("Canvas/fangJian").children[a[2]].getComponent(DTTS).LK();
           } 
        
            if (a[2]==7) {
                cc.find("Canvas/fangJian").children[1].getComponent(DTTS).LK();
           cc.find("Canvas/fangJian").children[3].getComponent(DTTS).LK();
           cc.find("Canvas/fangJian").children[5].getComponent(DTTS).LK();
            }
            if (a[2]==8) {
                cc.find("Canvas/fangJian").children[2].getComponent(DTTS).LK();
            cc.find("Canvas/fangJian").children[4].getComponent(DTTS).LK();
            cc.find("Canvas/fangJian").children[6].getComponent(DTTS).LK();
            }
  
  
  
  
  
  
        if (a[1]!=0) {
    cc.find("Canvas/fangJian").children[a[1]].getComponent(DTTS).BC(a[0]);
   } 

    if (a[1]==7) {
        cc.find("Canvas/fangJian").children[1].getComponent(DTTS).BC(a[0])
   cc.find("Canvas/fangJian").children[3].getComponent(DTTS).BC(a[0])
   cc.find("Canvas/fangJian").children[5].getComponent(DTTS).BC(a[0])
    }
    if (a[1]==8) {
        cc.find("Canvas/fangJian").children[2].getComponent(DTTS).BC(a[0])
    cc.find("Canvas/fangJian").children[4].getComponent(DTTS).BC(a[0])
    cc.find("Canvas/fangJian").children[6].getComponent(DTTS).BC(a[0])
    }


   

c+=a[3]
console.log(c)
}
this.FJN=true;
for(let a of data){
    if(a[1]==0){this.FJN=false;break;}
        }

        console.log(this.FJN)
if(c==6&&this.FJN &&this.FZ){ cc.find('Canvas/fangJian/kaishi').active=true
}
console.log(data)
   })
   
  
   
   
   
   
   
   
   KHD.Gammer.on('whoenter',(data:any)=>{
    
    
    console.log(data)
    
    switch (data[1]) {
    case 0:cc.find("Canvas/fangJian").children[data[2]].getComponent(DTTS).LK();
        
        break;
   case 7:cc.find("Canvas/fangJian").children[1].getComponent(DTTS).BC(data[0])
   cc.find("Canvas/fangJian").children[3].getComponent(DTTS).BC(data[0])
   cc.find("Canvas/fangJian").children[5].getComponent(DTTS).BC(data[0])
    cc.find("Canvas/fangJian").children[data[2]].getComponent(DTTS).LK();
    break;
    case 8:cc.find("Canvas/fangJian").children[2].getComponent(DTTS).BC(data[0])
    cc.find("Canvas/fangJian").children[4].getComponent(DTTS).BC(data[0])
    cc.find("Canvas/fangJian").children[6].getComponent(DTTS).BC(data[0])
     cc.find("Canvas/fangJian").children[data[2]].getComponent(DTTS).LK();
     break;
    default:cc.find("Canvas/fangJian").children[data[1]].getComponent(DTTS).BC(data[0]);cc.find("Canvas/fangJian").children[data[2]].getComponent(DTTS).LK();
        break;
   }})
        break;
}


     })


     
     console.log([FJname,PassW])
     KHD.Client.emit('connectroom',[FJname,PassW]);
 

}

static BPstart(){
    KHD.Gammer.on("gamemessage",(data)=>{
        let c=JSON.parse(data);
        
        KHD.JMessage.push(c)
        for(let v of KHD.JMessage){
           if(v[0]==KHD.dn+1){
            
            KHD.dn++
        
        let a=new GMessage(v[1].Type,v[1].Command,v[1].Content,v[1].from,v[1].SHtype)
        
        console.log(data)
        MessageCenter.SendMessage(a)
            KHD.JMessage.splice(KHD.JMessage.indexOf(v),1);
           } 
        
        }
        
        
        KHD.ln=KHD.dn;
        
            })
    
    
    cc.director.loadScene("chooseui",()=>{
       let a=new SHMessage("CH",0,0,1,"To")
        MessageCenter.SendMessage(a);
    })
    
}
static gamestart(){
  
        
    
}
static toFJ(n:number){
    KHD.Gammer.emit('gammername',[KHD.Cname,n]);

}


static send(sh:GMessage){

let a=[KHD.ln+1 ,sh,"1"]
let b=JSON.stringify(a)

KHD.Gammer.emit("gamemessage",b)
KHD.ln++;
}


//返回登录页面，原理参考LRAnseve里面的RSback
static Rsback(){
    let b=cc.find("Canvas/登录")
    for(let a of b.children){
        if(a.name=="account"){
            a.active=true;
        }
        else if(a.name=="passcount"){
            a.active=true;
        }
        else if(a.name=="New Label"){
            a.getComponent(cc.Label).string="登录";
        }
        else if(a.name=="load"){
            a.active=true;
        }
        else if(a.name=="register"){
            a.active=true;
        }
        else{
            a.active=false;
        }

    }
}

//更新货币
static Upmon(){
    cc.find("Canvas/大厅").getChildByName("货币").getChildByName("金币").getComponent(cc.Label).string=String(this.HB[0]);
    cc.find("Canvas/大厅").getChildByName("货币").getChildByName("钻石").getComponent(cc.Label).string=String(this.HB[1]);
    cc.find("Canvas/大厅").getChildByName("货币").getChildByName("猪币").getComponent(cc.Label).string=String(this.HB[2]);
}




static JR(FJname:string,PassW:string){


  
console.log(FJname)
       
        KHD.Client.emit('connectroom',[FJname,PassW]);


        KHD.Client.on('connectroom',(data:any)=>{
            console.log(data)
            cc.find("Canvas/New Node/New Label").getComponent(cc.Label).string=FJname
    switch (data[1]) {
        case 'F': cc.find("Canvas/CHU").children[2].getComponent(DTTS).WZTS("房间号或密码错误");
            
            break;
           
          
    
        default:
            KHD.Gammer=io.connect('http://118.25.16.45:3001/'+data[1],{
                reconnection: true,   // 启用自动重连
                reconnectionAttempts: 5,  // 最多尝试重连 5 次
                reconnectionDelay: 1000,  // 每次重连的时间间隔为 1 秒
                reconnectionDelayMax: 5000, // 每次重连的最大时间间隔为 5 秒
              });
    
        cc.find("Canvas/CHU").active=false;
        //房间重连
        KHD.Gammer.io.on('reconnect_attempt', (attemptNumber:any) => {
            console.log(`正在尝试第${attemptNumber}次重连...`);
        });
    
        KHD.Gammer.io.on('reconnect', (attemptNumber:any) => {
            console.log(`已重连至服务器，一共尝试了${attemptNumber}次`);
            // 执行自定义操作
        });
       KHD.Gammer.emit('gammername',[KHD.Cname,0,0,0]);
       KHD.Gammer.on('startBP',()=>{KHD.BPstart()})
       KHD.Gammer.on('all_people',(data:any)=>{
    let c=0
        for(let a of data){
            if (a[2]!=0) {
                cc.find("Canvas/fangJian").children[a[2]].getComponent(DTTS).LK();
               } 
            
                if (a[2]==7) {
                    cc.find("Canvas/fangJian").children[1].getComponent(DTTS).LK();
               cc.find("Canvas/fangJian").children[3].getComponent(DTTS).LK();
               cc.find("Canvas/fangJian").children[5].getComponent(DTTS).LK();
                }
                if (a[2]==8) {
                    cc.find("Canvas/fangJian").children[2].getComponent(DTTS).LK();
                cc.find("Canvas/fangJian").children[4].getComponent(DTTS).LK();
                cc.find("Canvas/fangJian").children[6].getComponent(DTTS).LK();
                }
      
      
      
      
      
      
            if (a[1]!=0) {
        cc.find("Canvas/fangJian").children[a[1]].getComponent(DTTS).BC(a[0]);
       } 
    
        if (a[1]==7) {
            cc.find("Canvas/fangJian").children[1].getComponent(DTTS).BC(a[0])
       cc.find("Canvas/fangJian").children[3].getComponent(DTTS).BC(a[0])
       cc.find("Canvas/fangJian").children[5].getComponent(DTTS).BC(a[0])
        }
        if (a[1]==8) {
            cc.find("Canvas/fangJian").children[2].getComponent(DTTS).BC(a[0])
        cc.find("Canvas/fangJian").children[4].getComponent(DTTS).BC(a[0])
        cc.find("Canvas/fangJian").children[6].getComponent(DTTS).BC(a[0])
        }
    
    
       
    
    c+=a[3]
    }
    this.FJN=true;
    for(let a of data){
        if(a[1]==0){this.FJN=false;break;}
            }
    
            console.log(this.FJN)
    if(c==6&&this.FJN &&this.FZ){ cc.find('Canvas/fangJian/kaishi').active=true
    }
    console.log(data)
       })
       
      
       
       
       
       
       
       
       KHD.Gammer.on('whoenter',(data:any)=>{
        
        
        console.log(data)
        
        switch (data[1]) {
        case 0:cc.find("Canvas/fangJian").children[data[2]].getComponent(DTTS).LK();
            
            break;
       case 7:cc.find("Canvas/fangJian").children[1].getComponent(DTTS).BC(data[0])
       cc.find("Canvas/fangJian").children[3].getComponent(DTTS).BC(data[0])
       cc.find("Canvas/fangJian").children[5].getComponent(DTTS).BC(data[0])
        cc.find("Canvas/fangJian").children[data[2]].getComponent(DTTS).LK();
        break;
        case 8:cc.find("Canvas/fangJian").children[2].getComponent(DTTS).BC(data[0])
        cc.find("Canvas/fangJian").children[4].getComponent(DTTS).BC(data[0])
        cc.find("Canvas/fangJian").children[6].getComponent(DTTS).BC(data[0])
         cc.find("Canvas/fangJian").children[data[2]].getComponent(DTTS).LK();
         break;
        default:cc.find("Canvas/fangJian").children[data[1]].getComponent(DTTS).BC(data[0]);cc.find("Canvas/fangJian").children[data[2]].getComponent(DTTS).LK();
            break;
       }})
            break;
    }
    
    
         })
    
    
         
         console.log([FJname,PassW])
         KHD.Client.emit('connectroom',[FJname,PassW]);
     
    
    }



}





     
    // console.log([FJname,PassW])

 
    







*/


}
